package lenovo.agriculture.adaper;

import java.util.List;

import lenovo.agriculture.activity.MainActivity;
import lenovo.agriculture.activity.GuideActivity;
import lenovo.agriculture.activity.R;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 自定义适配器
 * 
 * @author asus
 */

public class ViewPagerAdapter extends PagerAdapter {
	private Context mContext;
	private List<Integer> mList;
	private LayoutInflater mInflater;

	/**
	 * 构造函数
	 * 
	 * @param mContext
	 *            上下文
	 * @param mList
	 *            数据源
	 */
	public ViewPagerAdapter(Context mContext, List<Integer> mList) {
		this.mContext = mContext;
		this.mList = mList;
		this.mInflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	/**
	 * 数据源长度
	 * 
	 * @see android.support.v4.view.PagerAdapter#getCount()
	 */
	@Override
	public int getCount() {
		return mList.size();
	}

	/**
	 * 判断两个对象是否相同
	 * 
	 * @see android.support.v4.view.PagerAdapter#isViewFromObject(android.view.View,
	 *      java.lang.Object)
	 */
	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0.equals((View) arg1);
	}

	/**
	 * 销毁视图
	 * 
	 * @see android.support.v4.view.PagerAdapter#destroyItem(android.view.ViewGroup,
	 *      int, java.lang.Object)
	 */
	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		((ViewPager) container).removeView((View) object);
	}

	/**
	 * 获取Item的位置
	 * 
	 * @see android.support.v4.view.PagerAdapter#getItemPosition(java.lang.Object)
	 */
	@Override
	public int getItemPosition(Object object) {
		return POSITION_NONE;
	}

	/**
	 * 自定义视图
	 * 
	 * @see android.support.v4.view.PagerAdapter#instantiateItem(android.view.ViewGroup,
	 *      int)
	 */
	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		View view = (View) mInflater.inflate(R.layout.guide_item, container,
				false);
		RelativeLayout mLayout = (RelativeLayout) view
				.findViewById(R.id.picture);
		TextView mTextView = (TextView) view.findViewById(R.id.enter_tv);
		mLayout.setBackgroundResource(mList.get(position));
		mTextView.setBackgroundResource(R.drawable.control_bg);
		// 设置跳转按钮
		if (position == 2) {
			mTextView.setVisibility(View.VISIBLE);
			mTextView.setBackgroundColor(Color.GREEN);
		} else {
			mTextView.setVisibility(View.GONE);
		}
		// 跳转到主界面
		mTextView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				GuideActivity mActivity = (GuideActivity) mContext;
				mContext.startActivity(new Intent(mContext, MainActivity.class));
				mActivity.finish();
			}
		});
		((ViewPager) container).addView(view, 0);
		return view;
	}
}
